package com.own.component.common.login.parent;

import com.own.component.common.login.AuthenticationLoginUser;
import com.own.component.common.login.user.BaseLoginUser;
import com.own.component.common.login.user.BasePermissionUser;
import com.own.component.common.login.user.DefaultLoginUser;
import com.own.component.common.login.user.DefaultPermissionUser;
import com.own.component.common.login.util.BaseUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.function.Supplier;

/**
 * ParentAuthenticationDefaultCacheLoginUser
 *
 * @author chenxueli
 * @date 2023-08-10 16:27:42
 */
@Slf4j
public class ParentAuthenticationDefaultCacheLoginUser implements ParentAuthenticationLoginUser {

    private static final String LOGIN_USER_CACHE_KEY = "LOGIN_USER_CACHE_KEY";
    private static final String PERMISSION_USER_CACHE_KEY = "PERMISSION_USER_CACHE_KEY";

    private final AuthenticationLoginUser authenticationLoginUser;
    private final HttpServletRequest request;

    public ParentAuthenticationDefaultCacheLoginUser(AuthenticationLoginUser authenticationLoginUser, HttpServletRequest request) {
        this.authenticationLoginUser = authenticationLoginUser;
        this.request = request;
    }

    /**
     * 基本类用户信息
     *
     * @return 基本类用户信息
     */
    @Override
    public BaseLoginUser loginUser() {
        return cache(LOGIN_USER_CACHE_KEY, DefaultLoginUser.DEFAULT, authenticationLoginUser::loginUser);
    }

    /**
     * 权限类用户信息
     *
     * @param loginUser 登录的用户信息
     * @return 权限类用户信息
     */
    @Override
    public BasePermissionUser permissionUser(BaseLoginUser loginUser) {
        return cache(PERMISSION_USER_CACHE_KEY, DefaultPermissionUser.DEFAULT, () -> authenticationLoginUser.permissionUser(loginUser));
    }

    /**
     * 基本类用户信息
     *
     * @param userId 用户id
     * @return 基本类用户信息
     */
    @Override
    public List<BaseLoginUser> loginUser(Long userId) {
        return BaseUtil.get(null, () -> authenticationLoginUser.loginUser(userId));
    }

    /**
     * 权限类用户信息
     *
     * @param userId 用户id
     * @return 权限类用户信息
     */
    @Override
    public List<BasePermissionUser> permissionUser(Long userId) {
        return BaseUtil.get(null, () -> authenticationLoginUser.permissionUser(userId));
    }

    /**
     * 缓存信息
     *
     * @param key         缓存key
     * @param defaultUser 默认的用户信息
     * @param getFunction 获取方法
     * @param <T>         类型
     * @return 用户信息
     */
    @SuppressWarnings("all")
    private <T> T cache(String key, T defaultUser, Supplier<T> getFunction) {
        // 先去request的缓存中获取，如果存在则直接返回
        var cache = request.getAttribute(key);
        if (cache != null) {
            log.debug("获取了缓存中的用户信息");
            return (T) cache;
        }
        var user = getFunction.get();
        // 判断默认值
        var result = user == null ? defaultUser : user;
        // 存入request的缓存中
        request.setAttribute(key, result);
        return result;
    }

}
